package com.neusoft.cmsprojects.action;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.codec.digest.DigestUtils;
import org.apache.struts2.interceptor.RequestAware;
import org.apache.struts2.interceptor.SessionAware;

import com.neusoft.cmsprojects.model.Privilege;
import com.neusoft.cmsprojects.model.Role;
import com.neusoft.cmsprojects.model.TbUser;
import com.neusoft.cmsprojects.service.PrivilegeService;
import com.neusoft.cmsprojects.service.UserService;

public class LoginAction implements RequestAware, SessionAware{

   private static final long serialVersionUID = 1L;
	
	private TbUser user;
	private Map session;
	private Map request;
	@Resource(name="userservice")
	private UserService userservice;
	
	private HttpServletResponse response;
//	response= ServletActionContext.getResponse();
	private String msg;
	
	//登陆时间
	private String logintime;
	
	@Resource(name="privilegeservice")
	private PrivilegeService ps;
	
	private Integer[] privilegeIds;
	
	/**
	 * 登录
	 * 方法名：login<BR>
	 * 创建人：许耀东 <BR>
	 * 时间：2014年12月17日-下午2:03:33 <BR>
	 * @return String<BR>
	 * @throws Exception 
	 * @exception <BR>
	 * @since  1.0.0
	 */
	@SuppressWarnings("unused")
	public String login() throws Exception{
		user.setCpassword(DigestUtils.md5Hex(user.getCpassword()));
		TbUser  u=userservice.login(user);
		
		if(u.getCid()==null){
			request.put("msg","帐号或密码错误！");
		 return "login";
		}else{
		SimpleDateFormat formatter = new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); 
		Date currentTime = new Date();//得到当前系统时间 
		logintime= formatter.format(currentTime); //将日期时间格式化 
		
		for (Role  rol: u.getRole()) {
			if(rol.getPrivilege()!=null){
				privilegeIds=new Integer[rol.getPrivilege().size()];
				int index=0;
				for(Privilege pri:rol.getPrivilege()){
					privilegeIds[index++]=pri.getId();
				}
			}
		}
		
		List<Privilege> list=ps.findTopPrivilege();
		session.put("topPrivilegeListmenu", list);
		//保存本次登录时间
		user.setClogintime(logintime);
		user.setCid(u.getCid());
		userservice.updateLoginTime(user);
		session.put("user",u);
		session.put("logintime", logintime);
		session.put("pids", privilegeIds);
		return "index";
		}
		
	}
	/*跳转到登陆界面
	 * 
	 * 
	 */
	public String loginPage(){
		session.remove("user");
		return "login";
	}
	/**
	 * 退出登录
	 * 方法名：loginOut<BR>
	 * 创建人：许耀东 <BR>
	 * 时间：2014年12月18日-下午2:00:43 <BR>
	 * @return String<BR>
	 * @exception <BR>
	 * @since  1.0.0
	 */
	public String loginOut(){
		session.remove("user");
		return "login";
	}
	 
	
	//setter getter
	public TbUser getUser() {
		return user;
	}
	public void setUser(TbUser user) {
		this.user = user;
	}
	public HttpServletResponse getResponse() {
		return response;
	}
	public void setResponse(HttpServletResponse response) {
		this.response = response;
	}
	@Override
	public void setSession(Map session) {
		this.session=session;
	}
	@Override
	public void setRequest(Map  request) {
		this.request=request;
	}

	public String getMsg() {
		return msg;
	}

	public void setMsg(String msg) {
		this.msg = msg;
	}

	public String getLogintime() {
		return logintime;
	}

	public void setLogintime(String logintime) {
		this.logintime = logintime;
	}
	public PrivilegeService getPs() {
		return ps;
	}
	public void setPs(PrivilegeService ps) {
		this.ps = ps;
	}
	public Integer[] getPrivilegeIds() {
		return privilegeIds;
	}
	public void setPrivilegeIds(Integer[] privilegeIds) {
		this.privilegeIds = privilegeIds;
	}


}
